package flex.cc.store.controller;

import java.io.File;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import flex.cc.PublicUtil;
import flex.cc.store.service.CardExportService;
import flex.cc.util.FileDownloadUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import flex.cc.core.support.BaseController;
import flex.cc.store.dto.entity.StoreCouponEntity;
import flex.cc.store.service.StoreCouponService;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * @author WANGCONG KSY
 * 2021年12月30日下午13:41:56
 * @desc 卡券管理：查询、导出
 */
@RestController
@RequestMapping(value = "/coupon")
public class CardController extends BaseController{

	@Resource
	private StoreCouponService couponService;
    @Resource
    private CardExportService cardExportService;
	
	@PostMapping(value = "/queryListWithPage")
    @ApiOperation(httpMethod = "POST", value = "查询卡券列表")
    public Wrapper<IPage<StoreCouponEntity>> queryOrderListWithPage(@ApiParam(name = "storeCoupon", value = "卡券信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("查询卡券列表storeCoupon={}", reqVo);
        StoreCouponEntity storeCouponVo = initVo(reqVo);
        IPage<StoreCouponEntity> page = couponService.queryCardListWithPage(storeCouponVo);
        return WrapMapper.ok(page);
    }

    /**
     * @desc 初始化构建查询VO对象
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public StoreCouponEntity initVo(Map<String, Object> reqVo) {
        StoreCouponEntity storeCouponVo = new StoreCouponEntity();
        storeCouponVo.setCouponNum( PublicUtil.isNotEmpty(reqVo.get("couponNum")) ? reqVo.get("couponNum").toString() : null );
        storeCouponVo.setChannel( PublicUtil.isNotEmpty(reqVo.get("channel")) ? reqVo.get("channel").toString() : null );
        storeCouponVo.setServer( PublicUtil.isNotEmpty(reqVo.get("server")) ? reqVo.get("server").toString() : null );
        String status = PublicUtil.isNotEmpty(reqVo.get("status")) ? reqVo.get("status").toString() : null;
        String activityId = PublicUtil.isNotEmpty(reqVo.get("activityId"))? reqVo.get("activityId").toString() : null;
        storeCouponVo.setStartTime( PublicUtil.isNotEmpty(reqVo.get("startTime")) ? reqVo.get("startTime").toString() : null );
        storeCouponVo.setEndTime( PublicUtil.isNotEmpty(reqVo.get("endTime")) ? reqVo.get("endTime").toString() : null );
        // 卡券状态
        if(StringUtils.isNotEmpty(status)){
            storeCouponVo.setStatusArr(status.split(","));
        }
        // 卡券类型
        if(StringUtils.isNotEmpty(activityId)){
            storeCouponVo.setActivityIdArr(activityId.split(","));
        }
        storeCouponVo.setPageNum( Integer.parseInt(String.valueOf(reqVo.get("pageNum"))) );
        storeCouponVo.setPageSize( Integer.parseInt(String.valueOf(reqVo.get("pageSize"))) );
        
        String serviceType = PublicUtil.isNotEmpty(reqVo.get("serviceType")) ? reqVo.get("serviceType").toString() : null;//1-服务包、2-单项服务
        if(StringUtils.isNotEmpty(serviceType) && serviceType.equals("1")){
        	storeCouponVo.setServiceType("1");
        }else if(StringUtils.isNotEmpty(serviceType) && serviceType.equals("2")){
        	storeCouponVo.setServiceType("2");
        }
        
        return storeCouponVo;
    }

    @PostMapping(value = "/exportAll")
    @ApiOperation(httpMethod = "POST", value = "卡券全部导出")
    public void exportAll(HttpServletRequest request, HttpServletResponse response,
                          @ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo){
        logger.info("卡券全部导出requestVo={}", reqVo);
        StoreCouponEntity storeCouponVo = initVo(reqVo);
        storeCouponVo.setPageNum(1);
        if(Integer.parseInt(String.valueOf(reqVo.get("total")))== 0){
            storeCouponVo.setPageSize( 999 );
        }else{
            storeCouponVo.setPageSize( Integer.parseInt(String.valueOf(reqVo.get("total"))) );
        }
        String fileParentPath = System.getProperty("user.dir") + File.separator + "package" + File.separator + System.currentTimeMillis();
        String fileName = "卡券列表";
        cardExportService.downLoadSelection(request, response, storeCouponVo, fileParentPath, fileName, "2");
        String filepath = fileParentPath + File.separator + fileName + ".xlsx";
        FileDownloadUtil.downloadByFilepath(request, response, filepath);
    }

    @PostMapping(value = "/exportSelections")
    @ApiOperation(httpMethod = "POST", value = "卡券部分导出")
    public void exportSelections(HttpServletRequest request, HttpServletResponse response, @RequestBody StoreCouponEntity storePackageEntity){
        logger.info("卡券部分导出");
        String dataIds = storePackageEntity.getDataId();
        System.out.println(dataIds);
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(dataIds)){
            storePackageEntity.setDataIdArr(dataIds.split(","));
            storePackageEntity.setPageNum(1);
            storePackageEntity.setPageSize(dataIds.split(",").length);

            String fileParentPath = System.getProperty("user.dir") + File.separator + "package" + File.separator + System.currentTimeMillis();
            String fileName = "卡券列表";
            cardExportService.downLoadSelection(request, response, storePackageEntity, fileParentPath, fileName, "1");
            String filepath = fileParentPath + File.separator + fileName + ".xlsx";
            FileDownloadUtil.downloadByFilepath(request, response, filepath);
        }
    }
}
